<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Using Explicit Parameters with a Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Using Explicit Parameters with a Stored Procedure</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>Command Text: <b><span id="outCommandText" runat="server"></span></b></div>
<div>Input Parameter: <b><span id="outInParams" runat="server"></span></b></div>
<div>Output Parameters: <b><span id="outOutParams" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>

<script language="vb" runat="server">

Sub Page_Load()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect = ctlConnectStrings.OLEDBConnectionString
   outConnect.InnerText = strConnect 'and display it

   'specify the name of the stored procedure to use as the CommandText
   Dim strCommandText As String = "FindFromISBN"
   outCommandText.InnerText = strCommandText 'and display it

   'create a new Connection object using the connection string
   Dim objConnect As New OleDbConnection(strConnect)

   'create a new Command object and set the CommandType
   Dim objCommand As New OleDbCommand(strCommandText, objConnect)
   objCommand.CommandType = CommandType.StoredProcedure

   'create a variable to hold a Parameter object
   Dim objParam As OleDbParameter

   'create a new Parameter object named 'ISBN' with the correct data
   'type to match a SQL database 'varchar' field of 12 characters
   objParam = objCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12)

   'specify that it's an Input parameter and set the value
   objParam.Direction = ParameterDirection.Input
   objParam.Value = "1861002882"

   'create a new Parameter object named 'Title' with the correct data
   'type to match a SQL database 'varchar' field of 50 characters
   'and specify that it's an output parameter (so no value required)
   objParam = objCommand.Parameters.Add("Title", OleDbType.VarChar, 50)
   objParam.Direction = ParameterDirection.Output

   'create another output Parameter object named 'Date' with the correct
   'data type to match a SQL database 'datetime' field
   objParam = objCommand.Parameters.Add("Date", OleDbType.DBDate)
   objParam.Direction = ParameterDirection.Output

   'display the value of the input parameter
   outInParams.InnerText = "ISBN='" & objCommand.Parameters("ISBN").Value & "'"

   Try

      'open the connection and execute the command
      objConnect.Open()
      objCommand.ExecuteNonQuery()

   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing data</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'collect the values of the output parameters - note the use of
   'the ToString method as they will contain DBNull if there was no
   'match for the ISBN and this will cause an error if displayed
   Dim strTitle = objCommand.Parameters("Title").Value.ToString()
   Dim strDate = objCommand.Parameters("Date").Value.ToString()
   outOutParams.InnerHtml = "Title='" & strTitle & "' &nbsp; Date=" & strDate

End Sub
</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
